/*
 * @lc app=leetcode.cn id=101 lang=javascript
 *
 * [101] 对称二叉树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
// 递归版本
var isSymmetric = function(root) {
  return check(root, root)
}

var check = function (left, right) {
  if (!left && !right) return true
  if (!left || !right) return false
  return left.val === right.val && check(left.left, right.right) && check(left.right, right.left)
}
// @lc code=end
// 使用广度优先遍历，拿到每一层的节点，然后使用对撞指针进行对称判断
// var isSymmetric = function(root) {
//   if (!root) return false
//   const queue = []
//   queue.push(root)
//   while (queue.length) {
//     const list = [...queue]
//     queue.length = 0
//     let leftPointer = 0
//     let rightPointer = list.length - 1
//     while (leftPointer < rightPointer) {
//       let left = list[leftPointer] ? list[leftPointer].val : list[leftPointer]
//       let right = list[rightPointer] ? list[rightPointer].val : list[rightPointer]
//       leftPointer++
//       rightPointer--
//       if (left !== right) return false
//     }
//     list.forEach((node) => {
//       if (node) {
//         queue.push(node.left)
//         queue.push(node.right)
//       }
//     })
//   }
//   return true
// };